package com.yunnuo.system.operlog.service.impl;

import com.yunnuo.system.operlog.dao.ILogininforDao;
import com.yunnuo.system.operlog.entity.Logininfor;
import com.yunnuo.system.operlog.service.ILogininforService;
import com.yunnuo.system.comm.util.Empty;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.system.users.entity.PageParams;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.List;

/**
 * @author 魏洪福（15064000912@163.com）
 * @create 2018-08-31-15:28
 */
@Service
public class LogininforServiceImpl implements ILogininforService {

    @Autowired
    private ILogininforDao logininforDao;

    /**
     * 分页查询
     * @param pageParams
     * @param pageRequest
     * @return
     */
    @Override
    public TableDataInfo selecLogininforByPageParams(PageParams pageParams, PageRequest pageRequest) {
        TableDataInfo dataInfo=new TableDataInfo();
        Specification<Logininfor> specification=new Specification<Logininfor>() {
            @Override
            public Predicate toPredicate(Root<Logininfor> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namePath=root.get("loginName");
                String value=pageParams.getSearchValue();
                if (Empty.isNotEmpty(value)){
                    query.where(cb.like(namePath,"%"+value+"%"));
                }
                return null;
            }
        };
        Page<Logininfor> dictTypes=logininforDao.findAll(specification,pageRequest);
        dataInfo.setRows(dictTypes.getContent());
        dataInfo.setTotal(dictTypes.getTotalElements());
        return dataInfo;
    }

    @Override
    public void addVO(Logininfor vo) throws ServiceException {
        logininforDao.saveAndFlush(vo);
    }

    @Override
    public void updateVO(Logininfor vo) throws ServiceException {

    }

    @Override
    public void delList(String[] ids) throws ServiceException {

    }

    @Override
    public void deleteVo(String id) throws ServiceException {

    }

    @Override
    public List<Logininfor> findList() throws ServiceException {
        return logininforDao.findAll();
    }

    @Override
    public List<?> findList(Logininfor vo, String other) throws ServiceException {
        return null;
    }

    @Override
    public Logininfor findVO(String id) throws ServiceException {
        return logininforDao.findOne(id);
    }
}
